package com.support.android.designlibdemo;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author：JFWU on 2016/9/24 16:08
 * @E-mail：jeffy12138@126.com
 */
public class ViewPagerActivity extends AppCompatActivity {
    public static final String TAG = "MainActivity";
    private ViewPager pager;
    private int[] imgIds = {R.mipmap.cheese_1, R.mipmap.cheese_5, R.mipmap.cheese_2};
    private List<ImageView> imageViews;

    //各个页面的高度
    private int[] heights;
    private PagerAdapter adapter;

    float DownX;
    float DownY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);

        pager = (ViewPager) findViewById(R.id.pager_imgs);
        imageViews = new ArrayList<>(imgIds.length);
        heights = new int[imgIds.length];

        //因为现在还不知道ViewPager的宽度，所以要等ViewPager布局完成之后再进行一些初始化
        pager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //初始化各个页面
                for (int i = 0; i < imgIds.length; i++) {
                    ImageView imageView = new ImageView(ViewPagerActivity.this);
                    Drawable iconDrawable = getResources().getDrawable(imgIds[i]);
                    //计算该页面合适的高度，使得图片完整的显示，填充整个高度和宽度
                    heights[i] = (int) (pager.getWidth() / (float) iconDrawable.getIntrinsicWidth() * iconDrawable.getIntrinsicHeight());
                    imageView.setImageDrawable(iconDrawable);

                    imageViews.add(imageView);
                }
                adapter.notifyDataSetChanged();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    pager.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    pager.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
                //ViewPager的初始高度设置为第一个页面的高度
                ViewGroup.LayoutParams layoutParams = pager.getLayoutParams();
                layoutParams.height = heights[0];
                pager.setLayoutParams(layoutParams);
            }
        });


        adapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return imageViews.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView imageView = imageViews.get(position);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                container.addView(imageView);
                return imageView;
            }
        };
        pager.setAdapter(adapter);
        pager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        DownX = event.getX();//float DownX
                        DownY = event.getY();//float DownY
                        break;
                    case MotionEvent.ACTION_MOVE:
                        float moveX = event.getX() - DownX;//X轴距离
                        float moveY = event.getY() - DownY;//y轴距离

                        //为ViewPager设置高度
                        ViewGroup.LayoutParams params = pager.getLayoutParams();
                        params.height = (int) (moveY + params.height);
                        pager.setLayoutParams(params);
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }
                return false;
            }
        });
//        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                if (position == heights.length - 1) {
//                    return;
//                }
//                //计算ViewPager现在应该的高度,heights[]表示页面高度的数组。
//                int height = (int) (heights[position] * (1 - positionOffset) + heights[position + 1] * positionOffset);
//
//                //为ViewPager设置高度
//                ViewGroup.LayoutParams params = pager.getLayoutParams();
//                params.height = height;
//                pager.setLayoutParams(params);
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//            }
//        });

    }

}
